#!/usr/bin/env bash

cyan="\033[0;36m"
nocolor="\033[0m"

source "$(dirname "$0")/common.sh"
check_requirements

#-------------------------------------------------------------------------------
function usage() {
   echo
   echo -e "Make a Kong ${cyan}alpha|beta|rc${nocolor} release using this script:"
   echo ""
   echo "Usage:"
   if [ "$version" = "<x.y.z-alpha.n>" ]
   then
      echo "     List executed steps for a given release"
      echo "        $0 $version $1 $3"
      echo
   fi
   c=1
   step "switch"               "switch or create to the release branch"
   step "write_changelog"      "prepare the changelog"
   step "commit_changelog"     "commit the changelog"
   step "update_copyright"     "update copyright file"
   step "update_admin_api_def" "update Admin API definition"
   step "version_bump"         "bump and commit the version number"
   step "submit"               "push and submit a release PR"
   step "tag"                  "tag and sign the release candidate"
   if [ "$beta" == true ]
   then
     step "docs_pr"            "push and submit a docs.konghq.com PR for the release"
     step "approve_docker"     "update and submit a PR to Kong's docker-kong repo"
     step "merge_docker"       "merge, tag and sign Kong's docker-kong PR"
     step "merge_homebrew"     "humans approve and merge machine PR to homebrew-kong"
     step "merge_pongo"        "humans approve and merge machine PR to kong-pongo"
   fi
   exit 0
}

#-------------------------------------------------------------------------------
# Default help
#-------------------------------------------------------------------------------

if [ "$1" = "-h" ] || [ "$1" = "--help" ] || ! [ "$1" ]
then
   version="<x.y.z-alpha.n>"
   usage "$@"
fi


#-------------------------------------------------------------------------------
# Variables
#-------------------------------------------------------------------------------

version="${1:-0.0.0-alpha.0}"
step="$2"


xyzversion="${version%%-*}"
major=${xyzversion%%.*}
rest=${xyzversion#*.}
minor=${rest%%.*}
patch=${rest##*.}
prerelease=${version##*-}
rockspec="kong-$xyzversion$prerelease-0.rockspec"
branch="release/$xyzversion"
base="release/$major.$minor.x"

beta=false
if [[ $version == *"beta"* ]]; then
  beta=true
fi

if ! [[ "$version" =~ ^[0-9]+.[0-9]+.0-(alpha|beta|rc)\.[0-9]$ ]]
then
   die "first argument must be a version in x.y.z-(alpha|beta|rc).n format"
fi

if [ "$step" = "" ]
then
   usage "$@"
fi

EDITOR="${EDITOR-$VISUAL}"

case "$step" in
   #---------------------------------------------------------------------------
   switch)
      set -e
      git pull
      git checkout "$base" || true
      git checkout -B "$base" || true

      SUCCESS "Release branch is switched locally." \
              "You are ready to run the next step:" \
              "    $0 $version write_changelog"
      ;;
   #---------------------------------------------------------------------------
   write_changelog) write_changelog "$version" ;;
   commit_changelog) commit_changelog "$version" ;;
   update_copyright) update_copyright "$version" ;;
   update_admin_api_def) update_admin_api_def "$version" ;;

   #---------------------------------------------------------------------------
   version_bump)
      sed -i.bak 's/major = [0-9]*/major = '$major'/' kong/meta.lua
      sed -i.bak 's/minor = [0-9]*/minor = '$minor'/' kong/meta.lua
      sed -i.bak 's/patch = [0-9]*/patch = '$patch'/' kong/meta.lua
      sed -i.bak 's/--.*suffix.*$/suffix = "'$prerelease'"/' kong/meta.lua

      if ! [ -f "$rockspec" ]
      then
         old_rockspec=$(ls kong-*-0.rockspec | head -n 1)
         sed -i.bak 's/^version = ".*"/version = "'$xyzversion''$prerelease'-0"/' "$old_rockspec"
         sed -i.bak 's/^  tag = ".*"/  tag = "'$version'"/' "$old_rockspec"
      fi

      git diff kong/meta.lua $old_rockspec

      CONFIRM "If everything looks all right, press Enter to make the release commit" \
              "or Ctrl-C to cancel."

      git mv "$old_rockspec" "$rockspec"
      git add kong/meta.lua
      git add $rockspec

      git commit -m "release: $version"
      git log -n 1

      SUCCESS "Version bump for the release is now committed locally." \
              "You are ready to run the next step:" \
              "    $0 $version submit"
      ;;
   #---------------------------------------------------------------------------
   submit_release_pr) submit_release_pr "$base" "$version" ;;

   #---------------------------------------------------------------------------
   tag)
      CONFIRM "Press Enter to tag the prerelease (it is not actually merged)" \
              "or Ctrl-C to cancel."

      set -e
      git checkout "$base"
      git pull
      git tag -s "$version" -m "$version"
      git push origin "$version"

      make_github_release_file

      hub release create --prerelease -F release-$version.txt "$version"
      rm -f release-$version.txt

      SUCCESS "While the packages are being built continue to" \
            "     $0 $version docs_pr" \
            "After the packages are built continue to" \
            "Once they are built, you may run the following steps in parallel:" \
            "* 'approve_docker', then 'merge_docker'"
            "* 'merge_homebrew'" \
            "* 'merge_pongo'"
      ;;
   #---------------------------------------------------------------------------
   docs_pr) docs_pr "$branch" ;;
   approve_docker) approve_docker;;
   merge_docker) merge_docker "$branch" "$version" ;;
   merge_homebrew) merge_homebrew ;;
   merge_pongo) merge_pongo ;;
   #---------------------------------------------------------------------------
   *)
      die "Unknown step!"
      ;;
esac

